package com.hankz.cc.leetcode;

/**
 * 最长回文子串
 *
 * @auther zzc
 * @date 2021/3/12 17:04
 */
public class 最长回文子串 {
    public static void main(String[] args) {
        String s = "abcdcba";
        String str = geMaxHwStr(s);
        System.out.println(str);

    }

    //判断是否是回文数
    public static boolean checkHwStr(String s) {
        int length = s.length();
        for (int i = 0; i < (length / 2); i++) {
            if (s.charAt(i) != s.charAt(length - i - 1)) {
                return false;
            }
        }
        return true;
    }

    public static String geMaxHwStr(String s) {
        int length = s.length();
        String result = "";
        int max = 0;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                String test = s.substring(i, j);
                if (checkHwStr(test) && test.length() > max) {
                    result = s.substring(i, j);
                    max = Math.max(test.length(), max);
                }
            }
        }
        return result;
    }

}
